//
//  ScienceCell.m
//  NeteaseNewsLY
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ScienceCell.h"

@interface ScienceCell ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation ScienceCell
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
         self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT * 40, WIDTH * 375, HEIGHT * 667) style:UITableViewStylePlain];
        [self headerData];
        [self.contentView addSubview:_tableView];
        [_tableView release];
    }
    return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    _tableView.delegate = self;
    _tableView.dataSource = self;

    // 与首页10条信息相同 NewModel/NewTableViewCell
    [_tableView registerClass:[NewTableViewCell class] forCellReuseIdentifier:@"cell"];
    // 刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self headerData];
            [self.tableView.mj_header endRefreshing];
        });
    }];
    self.tableView.mj_header = header;
    // 加载
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self footerData];
        [self.tableView.mj_footer endRefreshing];
    }];
    self.tableView.mj_footer = footer;
}
// 刷新
- (void)headerData
{
    NSString *str = @"http://api.iclient.ifeng.com/ClientNews?id=KJ123,FOCUSKJ123&gv=4.5.1&av=4.5.1&uid=864595027630171&deviceid=864595027630171&proid=ifengnews&os=android_19&df=androidphone&vt=5&screen=1080x1920&publishid=2006&action=down";
    [AFHTTPHandler GETWithURL:str completion:^(id result) {
        NSArray *arr =result[0][@"item"];
        self.dataArr = [NewModel modelHandleWithArray:arr];
        [_tableView reloadData];
    }];
    
}
// 加载
- (void)footerData
{
    static NSInteger t = 2;
    NSString *str = @"http://api.iclient.ifeng.com/ClientNews?id=KJ123,FOCUSKJ123&gv=4.5.1&av=4.5.1&uid=864595027630171&deviceid=864595027630171&proid=ifengnews&os=android_19&df=androidphone&vt=5&screen=1080x1920&publishid=2006&action=down";
    NSString *urlStr = [str stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld", t]];
    [AFHTTPHandler GETWithURL:urlStr completion:^(id result) {
        NSArray *arr = [NewModel modelHandleWithArray:result[0][@"item"]];
        //        self.dataArr = [NewModel modelHandleWithArray:arr];
        for (NSInteger i = 0; i < arr.count; i++) {
            [_dataArr addObject:arr[i]];
        }
        [_tableView reloadData];
    }];
    t += 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewModel *m = _dataArr[indexPath.row];
    NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:m.thumbnail] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
    cell.title.text = m.title;
    cell.updateTime.text = [m.updateTime substringWithRange:NSMakeRange(11, 5)];
    cell.commentsall.text = [m.commentsall stringByAppendingString:@"跟帖"];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT * 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewModel *m = _dataArr[indexPath.row];
    [self.delegate scienceValue:m.commentsUrl];
}

@end
